package hik;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sun.jna.Memory;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.ByteByReference;

import hik.entity.preset.Region;
import hik.entity.preset.RegionCoordinates;
import hik.entity.preset.RegionCoordinatesList;
import hik.entity.preset.ThermometryRegion;
import hik.entity.preset.ThermometryRegionList;
import hik.entity.preset.ThermometryScene;
import hik.jpeg.CatchJpegWithData;
import hik.utils.JaxbUtil;

public class Demo {

	public static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;

	public static String ip = "192.168.100.27";
	public static String userName = "admin";
	public static String pwd = "abcd1234";
	public static short port = 8000;

	public static HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
	public static NativeLong userID = new NativeLong(-1);

	public static NativeLong realtimeRet = new NativeLong(-1);
	public static NativeLong manualHandle = new NativeLong(-1);

	public static String getPresetInfoXML = "";

	public static void main(String[] args) throws InterruptedException {

		if (args.length == 3) {
			ip = args[0];
			userName = args[1];
			pwd = args[2];
		}

		// 初始化 sdk
		boolean initRet = hCNetSDK.NET_DVR_Init();
		if (!initRet) {
			System.out.println("初始化海康SDK失败.");
			return;
		}
		connect();

		// 获取能力集
//		 getDeviceAbility();

		// 获取全屏测温
		getAllImageTemp();

		// 获取预置位信息
//		 String ret = getRulesISAPI("GET /ISAPI/Thermal/channels/2/thermometry/1");
//		 if (ret != null && !"".equals(ret)) {
//			ThermometryScene bean = JaxbUtil.converyToJavaBean(ret, ThermometryScene.class);
//			
//		}

		// 预置位实时测温
//		 ThermometryScene thermometryScene = generatePresetInfo();
//		 String initPresetInfo = converTomXml(thermometryScene);
////		 
//		 String strURL = "PUT /ISAPI/Thermal/channels/2/thermometry/1";
//		 setPresetInfoISAPI(strURL,initPresetInfo);
//		 startRealtimeCallback();

		// 获取实时测温xml
//		 getXmlISAPI("GET /ISAPI/Thermal/channels/2/thermometry/1/regions/4") ;
//		 getXmlISAPI("GET /ISAPI/Thermal/channels/2/thermometry/0") ;

//		 getXmlISAPI("GET /ISAPI/Thermal/manualThermometry/channels/2") ;

//		 startManualCallback();
//		 
//		 String setManual = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ManualThermometry version=\"2.0\" xmlns=\"http://www.hikvision.com/ver20/XMLSchema\"><normalizedScreenSize><normalizedScreenWidth>1000</normalizedScreenWidth><normalizedScreenHeight>1000</normalizedScreenHeight></normalizedScreenSize><ThermometryRuleList version=\"2.0\" xmlns=\"http://www.hikvision.com/ver20/XMLSchema\"><ThermometryRule><ruleId>6</ruleId><enabled>true</enabled><name>L01</name><type>line</type><Line><CalibratingCoordinates><CalibratingCoordinates><positionX>160</positionX><positionY>360</positionY></CalibratingCoordinates><CalibratingCoordinates><positionX>360</positionX><positionY>160</positionY></CalibratingCoordinates></CalibratingCoordinates></Line></ThermometryRule></ThermometryRuleList></ManualThermometry>";
//		 setXmlISAPI("PUT /ISAPI/Thermal/manualThermometry/channels/2", setManual);

		// 手动测温
//		 setManualTherm(1,"R");
//		 
//		 startManualCallback();
//		 
//		 Thread.sleep(5000);
//		 getXmlISAPI("GET /ISAPI/Thermal/manualThermometry/channels/2") ;
//		 ThermometryRegion tRegion = new ThermometryRegion(6);
//		 tRegion.setVersion("2.0");
//		 tRegion.setXmlns("http://www.hikvision.com/ver20/XMLSchema");
//		 tRegion.setEnabled(true);
//		tRegion.setName("马里奥");
//		tRegion.setEmissivity("0.96");
//		tRegion.setDistance(25);
//		tRegion.setReflectiveEnable(true);
//		tRegion.setReflectiveTemperature("25");
//		tRegion.setEmissivityMode(null);
//		tRegion.setDistanceUnit(null);
//		tRegion.setPoint(null);
//		tRegion.setRegion(null);
//		tRegion.setType(ThermometryRegion.TYPE_LINE);
//		
//		// 转换坐标
//
//		// 添加坐标数据
//		RegionCoordinates regionCoordinates1 = new RegionCoordinates(21, 350);
//		RegionCoordinates regionCoordinates2 = new RegionCoordinates(43, 545);
//		List<RegionCoordinates> RegionCoordinates = new ArrayList<>();
//		RegionCoordinates.add(regionCoordinates1);
//		RegionCoordinates.add(regionCoordinates2);
//
//		RegionCoordinatesList regionCoordinatesList = new RegionCoordinatesList();
//		regionCoordinatesList.setRegionCoordinatesList(RegionCoordinates);
//
//		Region region = new Region();
//		region.setRegionCoordinatesList(regionCoordinatesList);
//
//		tRegion.setRegion(region);
//	 
//		for (int i = 7; i < 21; i++) {
//			tRegion.setId(i);
//			String converTomXmlString = JaxbUtil.converTomXmlString(tRegion);
//			System.out.println("converTomXmlString: ==>> "+converTomXmlString);
//			setPresetInfoISAPI("PUT /ISAPI/Thermal/channels/2/thermometry/1/regions/"+i,converTomXmlString);
//		}

//		startRealtimeCallback();

		try {
			Thread.sleep(1000 * 5);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		disconnect();

	}

	/**
	 * 设备抓图能力集
	 */
	private static void getDeviceAbility() {
		System.out.println("获取设备抓图能力集");
		String xmlInput = "<?xml version=\"1.0\" encoding=\"utf-8\"?><JpegCaptureAbility version=\"2.0\"><channelNO>2</channelNO></JpegCaptureAbility>";
		int XML_ABILITY_OUT_LEN = 3 * 1024 * 1024;
		Pointer memory = new Memory(XML_ABILITY_OUT_LEN);
		System.out.println(memory.toString());
		ByteByReference byteRef = new ByteByReference();
		System.out.println("这里...");
		System.out.println(xmlInput);
		System.out.println("用户句柄:" + userID);
		boolean net_DVR_GetDeviceAbility = hCNetSDK.NET_DVR_GetDeviceAbility(userID, hCNetSDK.DEVICE_JPEG_CAP_ABILITY,
				xmlInput, xmlInput.getBytes().length, memory, XML_ABILITY_OUT_LEN);// ok
		System.out.println("获取能力集结果：" + net_DVR_GetDeviceAbility);
		int iErr = hCNetSDK.NET_DVR_GetLastError();
		System.out.print(iErr);
		System.out.println("---");
//     	hCNetSDK.NET_DVR_Logout(userID);
//     	hCNetSDK.NET_DVR_Cleanup();
		System.out.println(memory.getString(0));
//     	hCNetSDK.NET_DVR_GetDeviceAbility(lUserID, hCNetSDK.DEVICE_ENCODE_ALL_ABILITY_V20 , xmlInput, xmlInput.getBytes().length, byteRef, XML_ABILITY_OUT_LEN);
		String xmlOutPut = memory.getString(0);
		System.out.println("能力集内容：" + xmlOutPut);
//     	InputStream is = new ByteArrayInputStream(xmlOutPut.getBytes());
	}

	/**
	 * 生成预置位信息
	 * 
	 * @return
	 */
	private static ThermometryScene generatePresetInfo() {
		ThermometryScene thermometryScene = new ThermometryScene(1);
//		 RegionCoordinates regionCoordinates1 = new RegionCoordinates(230,900);
//		 RegionCoordinates regionCoordinates2 = new RegionCoordinates(10,900);
		RegionCoordinates regionCoordinates3 = new RegionCoordinates(10, 200);
		RegionCoordinates regionCoordinates4 = new RegionCoordinates(230, 200);
		List<RegionCoordinates> regionCoordinates = new ArrayList<>();
//		 regionCoordinates.add(regionCoordinates1);
//		 regionCoordinates.add(regionCoordinates2);
		regionCoordinates.add(regionCoordinates3);
		regionCoordinates.add(regionCoordinates4);
		RegionCoordinatesList regionCoordinatesList = new RegionCoordinatesList();
		regionCoordinatesList.setRegionCoordinatesList(regionCoordinates);
		Region region = new Region();
		region.setRegionCoordinatesList(regionCoordinatesList);
		ThermometryRegionList thermometryRegionList = thermometryScene.getThermometryRegionList();
		List<ThermometryRegion> thermometryRegions = thermometryRegionList.getThermometryRegion();
		ThermometryRegion thermometryRegion = thermometryRegions.get(9);
		thermometryRegion.setEnabled(true);
//		 thermometryRegion.setName("TT1中文1");
		thermometryRegion.setEmissivity("0.90");
		thermometryRegion.setDistance(19);
		thermometryRegion.setReflectiveEnable(false);
		thermometryRegion.setReflectiveTemperature("20");
		thermometryRegion.setType(ThermometryRegion.TYPE_LINE);
		thermometryRegion.setPoint(null);
		thermometryRegion.setRegion(region);
		thermometryRegions.add(thermometryRegion);
		return thermometryScene;
	}

	public static String converTomXml(ThermometryScene thermometryRegion) {
		String str = JaxbUtil.converTomXml(thermometryRegion);
		Pattern p = Pattern.compile("thermometryScene");
		Matcher m = p.matcher(str);
		str = m.replaceAll("ThermometryScene");
		System.out.println(str);
		return str;
	}

	private static void disconnect() {
		hCNetSDK.NET_DVR_StopRemoteConfig(realtimeRet);
		hCNetSDK.NET_DVR_StopRemoteConfig(manualHandle);
		hCNetSDK.NET_DVR_Logout(userID);
		hCNetSDK.NET_DVR_Cleanup();
//		RedisClient.jedisPool.destroy();
		System.out.println("销毁句柄");

	}

	public static void connect() {
		System.out.println("start connect : " + ip + "," + userName + "," + pwd);
		// 设置连接时间与重连时间
		hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
		hCNetSDK.NET_DVR_SetReconnect(10000, true);
		userID = hCNetSDK.NET_DVR_Login_V30(ip, port, userName, pwd, deviceInfo);
		int errCode = hCNetSDK.NET_DVR_GetLastError();
		System.out.println("After connect errCode: " + errCode);
	}

	/**
	 * 获取预置位信息
	 */
	public static void getPresetInfoISAPI() {
		HCNetSDK.NET_DVR_STRING_POINTER stringRequest = new HCNetSDK.NET_DVR_STRING_POINTER();
		stringRequest.read();
		String strURL = "GET /ISAPI/Thermal/channels/2/thermometry/1";
		stringRequest.byString = strURL.getBytes();
		stringRequest.write();

		HCNetSDK.NET_DVR_STRING_POINTER stringInBuffer = new HCNetSDK.NET_DVR_STRING_POINTER();
		stringInBuffer.read();
//        String strInbuffer = "xx";
//        stringInBuffer.byString = strInbuffer.getBytes();
		stringInBuffer.write();

		HCNetSDK.NET_DVR_XML_CONFIG_INPUT struXMLInput = new HCNetSDK.NET_DVR_XML_CONFIG_INPUT();
		struXMLInput.read();
		struXMLInput.dwSize = struXMLInput.size();
		struXMLInput.lpRequestUrl = stringRequest.getPointer();
		struXMLInput.dwRequestUrlLen = stringRequest.byString.length;
//        struXMLInput.lpInBuffer = stringInBuffer.getPointer();
//        struXMLInput.dwInBufferSize = stringInBuffer.byString.length;
		struXMLInput.write();

		HCNetSDK.NET_DVR_STRING_POINTER stringXMLOut = new HCNetSDK.NET_DVR_STRING_POINTER();
		stringXMLOut.read();
		HCNetSDK.NET_DVR_STRING_POINTER struXMLStatus = new HCNetSDK.NET_DVR_STRING_POINTER();
		struXMLStatus.read();

		HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT struXMLOutput = new HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT();
		stringInBuffer.read();
		struXMLOutput.dwSize = struXMLOutput.size();
		struXMLOutput.lpOutBuffer = stringXMLOut.getPointer();
		struXMLOutput.dwOutBufferSize = stringXMLOut.size();
		struXMLOutput.lpStatusBuffer = struXMLStatus.getPointer();
		struXMLOutput.dwStatusSize = struXMLStatus.size();
		stringInBuffer.write();

		if (!hCNetSDK.NET_DVR_STDXMLConfig(userID, struXMLInput, struXMLOutput)) {
			int iErr = hCNetSDK.NET_DVR_GetLastError();
			System.out.println("NET_DVR_STDXMLConfig失败，错误号：" + iErr);
			return;

		} else {
			stringXMLOut.read();
			String strOutXML = new String(stringXMLOut.byString).trim();
			getPresetInfoXML = strOutXML;
			System.out.println("获取 strOutXML:" + strOutXML);
			struXMLStatus.read();
			String strStatus = new String(struXMLStatus.byString).trim();
			System.out.println("获取 strStatus:" + strStatus);
		}
	}

	/**
	 * 获取isapi 参数
	 * 
	 * @param strURL
	 * @return
	 */
	public static String getXmlISAPI(String strURL) {
		String ret = "";
		HCNetSDK.NET_DVR_STRING_POINTER stringRequest = new HCNetSDK.NET_DVR_STRING_POINTER();
		stringRequest.read();
//        String strURL = "GET /ISAPI/Thermal/channels/2/thermometry/1/regions/1";
		stringRequest.byString = strURL.getBytes();
		stringRequest.write();

		HCNetSDK.NET_DVR_STRING_POINTER stringInBuffer = new HCNetSDK.NET_DVR_STRING_POINTER();
		stringInBuffer.read();
//        String strInbuffer = "xx";
//        stringInBuffer.byString = strInbuffer.getBytes();
		stringInBuffer.write();

		HCNetSDK.NET_DVR_XML_CONFIG_INPUT struXMLInput = new HCNetSDK.NET_DVR_XML_CONFIG_INPUT();
		struXMLInput.read();
		struXMLInput.dwSize = struXMLInput.size();
		struXMLInput.lpRequestUrl = stringRequest.getPointer();
		struXMLInput.dwRequestUrlLen = stringRequest.byString.length;
//        struXMLInput.lpInBuffer = stringInBuffer.getPointer();
//        struXMLInput.dwInBufferSize = stringInBuffer.byString.length;
		struXMLInput.write();

		HCNetSDK.NET_DVR_STRING_POINTER stringXMLOut = new HCNetSDK.NET_DVR_STRING_POINTER();
		stringXMLOut.read();
		HCNetSDK.NET_DVR_STRING_POINTER struXMLStatus = new HCNetSDK.NET_DVR_STRING_POINTER();
		struXMLStatus.read();

		HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT struXMLOutput = new HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT();
		stringInBuffer.read();
		struXMLOutput.dwSize = struXMLOutput.size();
		struXMLOutput.lpOutBuffer = stringXMLOut.getPointer();
		struXMLOutput.dwOutBufferSize = stringXMLOut.size();
		struXMLOutput.lpStatusBuffer = struXMLStatus.getPointer();
		struXMLOutput.dwStatusSize = struXMLStatus.size();
		stringInBuffer.write();

		if (!hCNetSDK.NET_DVR_STDXMLConfig(userID, struXMLInput, struXMLOutput)) {
			int iErr = hCNetSDK.NET_DVR_GetLastError();
			System.out.println("NET_DVR_STDXMLConfig失败，错误号：" + iErr);
			return ret;

		} else {
			stringXMLOut.read();
			String strOutXML = new String(stringXMLOut.byString).trim();
			ret = getPresetInfoXML = strOutXML;
			System.out.println("获取 strOutXML:" + strOutXML);

			struXMLStatus.read();
			String strStatus = new String(struXMLStatus.byString).trim();
			System.out.println("获取 strStatus:" + strStatus);
		}
		return ret;
	}

	/**
	 * 设置xml信息
	 * 
	 * @param strURL
	 * @param presetInfo
	 */
	public static void setXmlISAPI(String strURL, String presetInfo) {
		HCNetSDK.NET_DVR_STRING_POINTER stringRequest = new HCNetSDK.NET_DVR_STRING_POINTER();
		stringRequest.read();

		stringRequest.byString = strURL.getBytes();
		stringRequest.write();

		HCNetSDK.NET_DVR_STRING_POINTER stringInBuffer = new HCNetSDK.NET_DVR_STRING_POINTER();
		stringInBuffer.read();
		stringInBuffer.byString = presetInfo.getBytes();
		stringInBuffer.write();

		HCNetSDK.NET_DVR_XML_CONFIG_INPUT struXMLInput = new HCNetSDK.NET_DVR_XML_CONFIG_INPUT();
		struXMLInput.read();
		struXMLInput.dwSize = struXMLInput.size();
		struXMLInput.lpRequestUrl = stringRequest.getPointer();
		struXMLInput.dwRequestUrlLen = stringRequest.byString.length;
		struXMLInput.lpInBuffer = stringInBuffer.getPointer();
		struXMLInput.dwInBufferSize = stringInBuffer.byString.length;
		struXMLInput.write();

		HCNetSDK.NET_DVR_STRING_POINTER stringXMLOut = new HCNetSDK.NET_DVR_STRING_POINTER();
		stringXMLOut.read();
		HCNetSDK.NET_DVR_STRING_POINTER struXMLStatus = new HCNetSDK.NET_DVR_STRING_POINTER();
		struXMLStatus.read();

		HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT struXMLOutput = new HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT();
		stringInBuffer.read();
		struXMLOutput.dwSize = struXMLOutput.size();
		struXMLOutput.lpOutBuffer = stringXMLOut.getPointer();
		struXMLOutput.dwOutBufferSize = stringXMLOut.size();
		struXMLOutput.lpStatusBuffer = struXMLStatus.getPointer();
		struXMLOutput.dwStatusSize = struXMLStatus.size();
		stringInBuffer.write();

		if (!hCNetSDK.NET_DVR_STDXMLConfig(userID, struXMLInput, struXMLOutput)) {
			int iErr = hCNetSDK.NET_DVR_GetLastError();
			System.out.println("NET_DVR_STDXMLConfig失败，错误号：" + iErr);
			return;
		} else {
			stringXMLOut.read();
			String strOutXML = new String(stringXMLOut.byString).trim();
			getPresetInfoXML = strOutXML;
			System.out.println("设置 strOutXML:" + strOutXML);
			struXMLStatus.read();
			String strStatus = new String(struXMLStatus.byString).trim();
			System.out.println("设置 strStatus:" + strStatus);
		}
	}

	/**
	 * 建立实时长连接
	 */
	public static void startRealtimeCallback() {
		System.out.println(" start startRealtimeCallback...");
		HCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND realTimeThermometryCond = new HCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND();
		realTimeThermometryCond.dwSize = realTimeThermometryCond.size();
		realTimeThermometryCond.byRuleID = 0; // 规则ID，0代表获取全部规则，具体规则ID从1开始
		realTimeThermometryCond.dwChan = 2;// 通道号，从1开始，0xffffffff代表获取全部通道
//			realTimeThermometryCond.byMode = 1; //1- 定时模式，2- 温差模式
		realTimeThermometryCond.write();
		Pointer lpConfig = realTimeThermometryCond.getPointer();

		RealtimeCallback fRemoteConfigCallback = new RealtimeCallback();

		boolean ConnectTime = hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
		System.out.println("ConnectTime: " + ConnectTime);
		boolean Reconnect = hCNetSDK.NET_DVR_SetReconnect(10000, true);
		System.out.println("Reconnect: " + Reconnect);

		realtimeRet = hCNetSDK.NET_DVR_StartRemoteConfig(userID, HCNetSDK.NET_DVR_GET_REALTIME_THERMOMETRY, lpConfig,
				realTimeThermometryCond.size(), fRemoteConfigCallback, null);
		System.out.println("startRealtimeCallback,error code:" + hCNetSDK.NET_DVR_GetLastError());
	}

	/**
	 * 设置手动测温规则
	 * 
	 * @param device
	 * @param ruleID
	 * @param ana_str
	 */
	public static void setManualTherm(int ruleID, String type) {
		HCNetSDK.INT_ARRAY ptrInt = new HCNetSDK.INT_ARRAY(1);
		ptrInt.iValue[0] = 2;
		ptrInt.write();
		// 手动测温规则配置
		HCNetSDK.NET_SDK_MANUAL_THERMOMETRY struManualTherm = new HCNetSDK.NET_SDK_MANUAL_THERMOMETRY();
		struManualTherm.read();
		struManualTherm.dwSize = struManualTherm.size();
		struManualTherm.dwChannel = 2;
		struManualTherm.byThermometryUnit = 0; // 测温单位: 0-摄氏度（℃），1-华氏度（℉），2-开尔文(K)
//		struManualTherm.struRuleInfo.byRuleID = (byte) ruleID;
		struManualTherm.struRuleInfo.byEnable = 1;
		for (int j = 0; j < struManualTherm.struRuleInfo.szRuleName.length; j++) {
			struManualTherm.struRuleInfo.szRuleName[j] = 0;
		}

		byte[] ruleNameBytes = (ruleID + type).getBytes();
		for (int j = 0; j < ruleNameBytes.length; j++) {
			struManualTherm.struRuleInfo.szRuleName[j] = ruleNameBytes[j];
		}

		if ("R".equals(type.toUpperCase())) {
			// 框
			struManualTherm.struRuleInfo.byRuleCalibType = 1;
			struManualTherm.struRuleInfo.struRegionTherm.struRegion.dwPointNum = 4;
			struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[0].fX = struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[3].fX = 0.33F;

			struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[0].fY = struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[1].fY = 0.13F;

			struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[1].fX = struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[2].fX = 0.59F;

			struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[2].fY = struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[3].fY = 0.11F;
		}
		if ("L".equals(type.toUpperCase())) {
			// 线
			struManualTherm.struRuleInfo.byRuleCalibType = 2;
			struManualTherm.struRuleInfo.struRegionTherm.struRegion.dwPointNum = 2;
			struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[0].fX = 0.33F;

			struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[0].fY = 0.13F;

			struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[1].fX = 0.59F;

			struManualTherm.struRuleInfo.struRegionTherm.struRegion.struPos[1].fY = 0.11F;
		}
		if ("P".equals(type.toUpperCase())) {
			// 点
			struManualTherm.struRuleInfo.byRuleCalibType = 0;
			struManualTherm.struRuleInfo.struPointTherm.struPoint.fX = 0.67F;
			struManualTherm.struRuleInfo.struPointTherm.struPoint.fY = 0.47F;
		}

		struManualTherm.write();

		HCNetSDK.BYTE_ARRAY ptrByte = new HCNetSDK.BYTE_ARRAY(1024);

		HCNetSDK.NET_DVR_STD_CONFIG stdCfg = new HCNetSDK.NET_DVR_STD_CONFIG();
		stdCfg.lpCondBuffer = ptrInt.getPointer();
		stdCfg.dwCondSize = ptrInt.size();
		stdCfg.lpInBuffer = struManualTherm.getPointer();
		stdCfg.dwInSize = struManualTherm.size();
		stdCfg.lpStatusBuffer = ptrByte.getPointer();
		stdCfg.dwStatusSize = ptrByte.size();
		stdCfg.write();
		boolean bRet = hCNetSDK.NET_DVR_SetSTDConfig(userID, HCNetSDK.NET_DVR_SET_MANUALTHERM, stdCfg);
		stdCfg.read();
		struManualTherm.read();
		ptrByte.read();

		System.out.println("手动测温禁用设置：" + bRet + " , errorCode：" + hCNetSDK.NET_DVR_GetLastError());
	}

	/**
	 * 设置手动测温回调
	 * 
	 * @param device
	 * @param fRemoteConfigCallback 回调函数
	 * @return
	 */
	public static NativeLong startManualCallback() {
		ManualCallback fRemoteConfigCallback = new ManualCallback();
		// 启动手动测温
		HCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND struThermCond = new HCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND();
		struThermCond.dwSize = struThermCond.size();
		struThermCond.byRuleID = 0; // 规则ID，0代表获取全部规则，具体规则ID从1开始
		struThermCond.dwChan = 2; // 从1开始，0xffffffff代表获取全部通道
		struThermCond.byMode = 1; // 1- 定时模式，2- 温差模式
		struThermCond.write();
		Pointer lpConfig = struThermCond.getPointer();

		boolean ConnectTime = hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
		System.out.println("ConnectTime: " + ConnectTime);
		boolean Reconnect = hCNetSDK.NET_DVR_SetReconnect(10000, true);
		System.out.println("Reconnect: " + Reconnect);

		NativeLong ManualHandle = hCNetSDK.NET_DVR_StartRemoteConfig(userID, HCNetSDK.NET_DVR_GET_MANUALTHERM_INFO,
				lpConfig, struThermCond.size(), fRemoteConfigCallback, null);
		if (ManualHandle.longValue() >= 0) {
			System.out.printf("NET_DVR_GET_MANUALTHERM_INFO successful, error code: %d\n",
					hCNetSDK.NET_DVR_GetLastError());
		} else {
			System.out.printf("NET_DVR_GET_MANUALTHERM_INFO is failed!");
		}
		return ManualHandle;
	}

	/**
	 * 获取全屏测温
	 */
	public static void getAllImageTemp() {
		// 全屏测温抓图
		long startl = System.currentTimeMillis();
		HCNetSDK.NET_DVR_JPEGPICTURE_WITH_APPENDDATA lpJpegWithAppend = new HCNetSDK.NET_DVR_JPEGPICTURE_WITH_APPENDDATA();
//        lpJpegWithAppend.read();
		lpJpegWithAppend.dwSize = lpJpegWithAppend.size();
		lpJpegWithAppend.dwChannel = 2;
//        lpJpegWithAppend.write();
		NativeLong chanLong = new NativeLong(2);
		boolean is = hCNetSDK.NET_DVR_CaptureJPEGPicture_WithAppendData(userID, chanLong, lpJpegWithAppend);
		System.out.println("全屏测温抓图错误码：" + hCNetSDK.NET_DVR_GetLastError());
		lpJpegWithAppend.read();
		if (is) {
			System.out.println("cost:" + (System.currentTimeMillis() - startl));

			System.out.println("jpeg-byteLength:" + lpJpegWithAppend.dwJpegPicLen + ";");
			System.out.println(
					"width: " + lpJpegWithAppend.dwJpegPicWidth + " ; height:" + lpJpegWithAppend.dwJpegPicHeight);

			System.out.println("temp-byteLength:" + lpJpegWithAppend.dwP2PDataLen + ";");

			// 保存图片
			BufferedOutputStream outputStream = null;
			try {
				outputStream = new BufferedOutputStream(
						new FileOutputStream(new File("c:/img/" + System.currentTimeMillis() / 1000 + "-2.jpeg")));
				outputStream.write(lpJpegWithAppend.pJpegPicBuff.getByteArray(0, lpJpegWithAppend.dwJpegPicLen), 0,
						lpJpegWithAppend.dwJpegPicLen);
				outputStream.flush();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (outputStream != null) {
					try {
						outputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

			byte[] points = new byte[4];
			float float1;
			// 保存温度数据
			File file = new File("c:/img/" + System.currentTimeMillis() / 1000 + "-1.txt");
			FileWriter out = null;
			BufferedWriter bw = null;
			
			File file1 = new File("c:/img/" + System.currentTimeMillis() / 1000 + "-5.txt");
			FileWriter out1 = null;
			BufferedWriter bw1 = null;

			try {
				if (!file.exists())
					file.createNewFile();
				if (!file1.exists())
					file1.createNewFile();

				out = new FileWriter(file);
				out1 = new FileWriter(file1);
				bw = new BufferedWriter(out);
				bw1 = new BufferedWriter(out1);
				int j = 0;
				byte[] byteArray = lpJpegWithAppend.pP2PDataBuff.getByteArray(0, lpJpegWithAppend.dwP2PDataLen);
				short[] data = new short[byteArray.length / 4];
				int k = 0;
				for (int i = 0; i < byteArray.length;) {
					points[0] = byteArray[i];
					points[1] = byteArray[i + 1];
					points[2] = byteArray[i + 2];
					points[3] = byteArray[i + 3];
					float1 = getFloat(points);
					data[k] = (short) float1;
					bw.write(data[k] + "\t");
					bw1.write(float1 + "\t");
					k++;
					if (j == lpJpegWithAppend.dwJpegPicWidth) {
						j = 0;
						bw.newLine();
						bw1.newLine();
					}
					j++;
					i = i + 4;
				}
				CatchJpegWithData.writeFile("c:/img/" + System.currentTimeMillis() / 1000 + "-4.fir",
						(short) lpJpegWithAppend.dwJpegPicHeight, (short) lpJpegWithAppend.dwJpegPicWidth, data);

				bw.flush();
				bw1.flush();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (bw != null) {
					try {
						bw.close();
						bw1.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (out != null) {
					try {
						out.close();
						out1.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		System.out.println("cost:" + (System.currentTimeMillis() - startl));

	}

	public static float getFloat(byte[] b) {
		int accum = 0;
		accum = accum | (b[0] & 0xff) << 0;
		accum = accum | (b[1] & 0xff) << 8;
		accum = accum | (b[2] & 0xff) << 16;
		accum = accum | (b[3] & 0xff) << 24;
		return Float.intBitsToFloat(accum);
	}

}
